//
//  CPContentController.m
//  ChinaPoem
//
//  Created by 赵俊明 on 2019/3/4.
//  Copyright © 2019 赵俊明. All rights reserved.
//

#import "CPContentController.h"
#import "CPContentTextView.h"
#import "CPSHAREController.h"
#import "UITextView+CPCapture.h"
@interface CPContentController ()<UITextViewDelegate>
@property (nonatomic, weak) CPContentTextView *poem;
@property (nonatomic, weak) UIScrollView *scroView;
@property(nonatomic, assign) BOOL saveStatus;
@end

@implementation CPContentController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [[CPSQLTools cp_sharedInstance] cp_insert:_model tName:@"history" save_sql:nil];
}

- (void)saveRight:(UIBarButtonItem *)sender
{
    if (_saveStatus) {
        [[CPSQLTools cp_sharedInstance] cp_delete:_model tName:@"save"];
    }else{
        NSString *savesql = @"";
        // (pk, dynasty, title, type, author, explanation, content, appreciation, timeNum)
        [[CPSQLTools cp_sharedInstance] cp_insert:_model tName:@"save" save_sql:savesql];
    }
    self.saveStatus = !self.saveStatus;
    JMSelf(ws);
    [self cp_show:_saveStatus?@"收藏":@"取消" msg:_saveStatus?@"添加收藏成功":@"取消收藏成功" cancel:NO handle:^(BOOL isLive) {
        NSString *saveIma = ws.saveStatus?@"like_fill":@"like";
        UIBarButtonItem *save = self.navigationItem.rightBarButtonItems.lastObject;
        save.image = [UIImage imageNamed:saveIma];
    }];
}

- (void)shareRight:(UIBarButtonItem *)sender
{
    UIImage *image = [_poem screenShotImage];
    [self cp_share:nil image:image handle:^(UIActivityType  _Nullable activityType, BOOL completed) {}];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = _model.cp_title;
    
    _saveStatus = [[CPSQLTools cp_sharedInstance] checkIsSave:_model];
    NSString *saveIma = _saveStatus?@"like_fill":@"like";
    UIBarButtonItem *save = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:saveIma] style:UIBarButtonItemStylePlain target:self action:@selector(saveRight:)];
    UIBarButtonItem *share = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"share"] style:UIBarButtonItemStylePlain target:self action:@selector(shareRight:)];
    self.navigationItem.rightBarButtonItems = @[share,save];
    
    CPContentTextView *poem = [[CPContentTextView alloc] initWithFrame:CGRectMake(10, 74, kW-50, kH-74-20)];
    [poem cp_contentText:_model.cp_content fontSize:26];
    poem.textAlignment = NSTextAlignmentCenter;
    poem.delegate = self;
    [self.view addSubview:poem];
    self.poem = poem;
    [poem mas_makeConstraints:^(MASConstraintMaker *make) {
        if (@available(iOS 11.0, *)) {
            make.top.mas_equalTo(self.view.mas_safeAreaLayoutGuideTop).offset(10);
            make.bottom.mas_equalTo(self.view.mas_safeAreaLayoutGuideBottom).offset(-10);;
        } else {
            make.top.mas_equalTo(self.view.mas_top).offset(10);
            make.bottom.mas_equalTo(self.view.mas_bottom).offset(-10);;
        }
        make.left.mas_equalTo(self.view).offset(10);
        make.right.mas_equalTo(self.view).offset(-40);
    }];
    
    CPNoteView *note = [[CPNoteView alloc] initWithFrame:CGRectMake(kW-44, kH/2-103, 34, 206)];
    note.poemSource = ^(NSInteger sType) {
        [self poemFromSource:sType];
    };
    note.titles = @[@"诗文", @"注释", @"赏析", @"作者"];
    [self.view addSubview:note];
    [note mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.view);
        make.right.mas_equalTo(self.view).offset(-4);
        make.width.mas_equalTo(34);
        make.height.mas_equalTo(206);
    }];
}

- (void)poemFromSource:(NSInteger)sourceType
{
    if (sourceType == 200) {
        
        [_poem cp_contentText:_model.cp_content fontSize:26];
        _poem.textAlignment = NSTextAlignmentCenter;
        
    }else if (sourceType == 201){
        
        [_poem cp_explanationText:_model.cp_explanation fontSize:17.0];
        _poem.textAlignment = NSTextAlignmentLeft;
        
    }else if (sourceType == 202){
        
        [_poem cp_explanationText:_model.cp_appreciation fontSize:17.0];
        _poem.textAlignment = NSTextAlignmentLeft;
        
    }else if (sourceType == 203){
        
        CPModel *model = [[CPSQLTools cp_sharedInstance] cp_authorInfo:_model.cp_author].firstObject;
        [_poem cp_explanationText:model.cp_content fontSize:17.0];
        _poem.textAlignment = NSTextAlignmentLeft;
        
    }else if (sourceType == 204){
        
        
    }
}

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
    return YES;
}

@end
